/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FRC2856.Robot;

import com.sun.squawk.microedition.io.FileConnection;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;

/**
 *
 * @author Henry
 */
public class FileHandler 
{

    public String filename = RobotMap.fileName;
    public FileConnection fc;
    public boolean usable = true;
    
    public FileHandler() 
    {
        try {
            fc = (FileConnection) Connector.open("file:////" + filename);

            if (!fc.exists()) 
            {
                System.out.println("AUTO file doesn't exist! I don't have a purpose anymore :(");
                usable = false;
            }
        } catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    /**
     * Reads the file in as a string.
     * @return the contents of the file as a string or null if the file doesn't exist or there was an error reading the file
     */
    public String readFile()
    {
        String output = null;
        if (usable)// if auto.txt exists
        {
            try {
                InputStream is = fc.openInputStream();
                byte b[] = new byte[1024];
                int length = is.read(b, 0, 1024);
                output = new String(b, 0, length);
                is.close();
                fc.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return output;
    }
}